// 实现本地身份验证策略
import { Strategy } from 'passport-local';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, NotFoundException } from '@nestjs/common';
import { AuthService } from 'src/services/auth.service';

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(private authService: AuthService) {
    super({
      usernameField: 'username',
      passwordField: 'pw',
    });
  }

  async validate(username: string, pw: string): Promise<any> {
    const user = await this.authService.validateUser(username, pw);
    return user; // 返回值会自动挂载到 req.user 上
  }
}
